package com.mioto.ld.module.product.controller;

import cn.hutool.core.util.StrUtil;
import com.mioto.ld.module.product.model.ProductTypePagerVO;
import org.springframework.web.bind.annotation.*;
import com.mioto.ld.module.product.model.ProductType;
import com.mioto.ld.module.product.service.IProductTypeService;
import com.mioto.ld.result.ResultData;
import com.mioto.ld.result.SystemTip;
import com.mioto.ld.component.BasePager;
import com.mioto.ld.exception.BasicException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 *
 * @author mioto
 * @date 2024-05-14 21:05:42
 */
@RestController
@RequestMapping("productType")
@Api(tags = "产品类别管理")
public class ProductTypeController {
    @Resource
    private IProductTypeService productTypeService;

    @GetMapping
    @ApiOperation(value="查询产品类别列表",response = ProductType.class)
    public ResultData list (HttpServletRequest request,ProductType productType){
        return ResultData.success(productTypeService.findList(productType));
    }

    @GetMapping("/pager")
    @ApiOperation(value="分页查询产品类别",response = ProductType.class)
    public ResultData pager (HttpServletRequest request, ProductType productType,BasePager basePager){
        PageHelper.startPage(basePager.getPage(), basePager.getRows(), basePager.getSortBy());
        List<ProductTypePagerVO> list = productTypeService.findPager(productType);
        PageInfo<ProductTypePagerVO> pageInfo = new PageInfo<>(list);
        Map<String, Object> result = new HashMap<>(4);
        result.put("count", pageInfo.getTotal());
        result.put("list", pageInfo.getList());
        return ResultData.success(result);
    }

    @GetMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名查询产品类别",response = ProductType.class)
    public ResultData findByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return ResultData.success(productTypeService.findByColumn(StrUtil.toUnderlineCase(column),value));
    }

    @PostMapping
    @ApiOperation(value="新增产品类别")
    public ResultData add (ProductType productType){
        return Optional.of(productTypeService.insert(productType))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(productType))
                .orElseThrow(() -> new BasicException(SystemTip.INSERT_FAIL));
    }

    @PatchMapping
    @ApiOperation(value="更新产品类别 局部更新,忽略空值")
    public ResultData updateIgnoreNull (ProductType productType){
        return Optional.of(productTypeService.updateIgnoreNull(productType))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(productType))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @PutMapping
    @ApiOperation(value="更新产品类别 整体更新")
    public ResultData update (ProductType productType){
        return Optional.of(productTypeService.update(productType))
                .filter(count -> count > 0)
                .map(count -> ResultData.success(productType))
                .orElseThrow(() -> new BasicException(SystemTip.UPDATE_FAIL));
    }

    @DeleteMapping("/{column}/{value}")
    @ApiOperation(value="通过字段名删除产品类别")
    public ResultData deleteByColumn (@PathVariable("column")String column,@PathVariable("value")Object value){
        return Optional.of(productTypeService.deleteByColumn(StrUtil.toUnderlineCase(column),value))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }

    @DeleteMapping("/batch")
    @ApiOperation(value="批量删除产品类别")
    public ResultData batchDelete (Integer... ids){
        return Optional.of(productTypeService.batchDelete(ids))
                .filter(count -> count > 0)
                .map(count -> ResultData.success())
                .orElseThrow(() -> new BasicException(SystemTip.DELETE_FAIL));
    }
}